﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Text.RegularExpressions;
using System.IO;

namespace WikiExtractor
{
    class Extractor
    {
        XmlTextReader SkipSiteInfo(XmlTextReader reader)
        {
            if (!reader.Read()) throw new Exception();
            while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name == "siteinfo"))
            {
                reader.Read();
            }
            return reader;
        }

        IEnumerable<WikiArticle> ExtractArticles(XmlTextReader reader)
        {
            while (reader.ReadToFollowing("page"))
            {
                if (!reader.ReadToDescendant("title")) throw new Exception();
                if (!reader.Read()) throw new Exception();
                if (reader.NodeType != XmlNodeType.Text) throw new Exception();
                var title = reader.Value;
                if (!reader.ReadToFollowing("text")) throw new Exception();
                if (!reader.Read()) throw new Exception();
                var text = "";
                switch (reader.NodeType)
                {
                    case XmlNodeType.Text:
                        text = reader.Value;
                        break;
                    case XmlNodeType.Whitespace:
                        text = "";
                        break;
                    default: throw new Exception("MatchFailureException");
                }

                if (!reader.Read()) throw new Exception();
                while (reader.NodeType != XmlNodeType.EndElement || reader.Name != "page")
                {
                    if (!reader.Read()) throw new Exception();
                }

                yield return new WikiArticle 
                { 
                    Title = title,
                    Text = text
                };
            }
        }

        public IEnumerable<WikiArticle> ExtractArticles(string wikiFile)
        {
            var reader = new XmlTextReader(wikiFile);
            reader = SkipSiteInfo(reader);

            foreach (var article in ExtractArticles(reader))
            {
                yield return article;
            }

            reader.Close();
        }
    }
}
